package C006;

/**
 * ClassName: ExtremePoint
 * Package: C005
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/11/28 16:15
 * @Version 1.0
 * @Since 1.0
 */
public class ExtremePoint {
    public static void main(String[] args) {
        int[] arr = {3,4,3,2,1};
        System.out.println(findExtremePoint(arr));
    }

    public static int findExtremePoint(int[] arr) {
        if(arr.length == 1) {
            return 0;
        }
        if(arr[0] > arr[1]) {
            return 0;
        }

        if(arr[arr.length - 1] > arr[arr.length - 2]) {
            return arr.length - 1;
        }

        int left = 0;
        int right = arr.length - 1;
        int midIndex = 0;
        while(left <= right) {
            midIndex = left + ((right - left) >> 1);
            if(arr[midIndex] > arr[midIndex + 1] && arr[midIndex] > arr[midIndex - 1]) {
                return midIndex;
            }else if(arr[midIndex - 1] < arr[midIndex]) {
                left = midIndex;
            } else {
                right = midIndex;
            }
        }
        return midIndex;
    }
}
